(function(){
  var loadTimeout = null;
  var callbackDone = false;

  // 监听页面报错
  // 错误集合，所在文件，行，列，错误信息
  function errorHandle(ev, source, lineno, clono, error){
    console.error('👀 Init Error: ', ev, source, lineno, clono, error);
    var element = document.createElement('div');
    element.style.position = 'fixed';
    element.style.top = '0';
    element.style.left = '0';
    element.style.right = '0';
    element.style.bottom = '0';
    element.style.zIndex = '29999';
    element.style.backgroundColor = '#222429';
    element.style.display = 'flex';
    element.style.justifyContent = 'center';
    element.style.alignItems = 'center';
    element.style.wordBreak = 'break-word';

    var inner = document.createElement('div');
    inner.style.color = '#fff';
    inner.style.fontFamily = 'zpix, sans-serif';
    inner.style.fontSize = '15px';
    inner.style.padding = '30px';
    element.appendChild(inner);

    var heading = document.createElement('h3');
    heading.style.color = '#ef5072';
    heading.innerHTML = 'Error';
    heading.style.marginBottom = '40px';
    heading.style.fontSize = '45px';
    inner.appendChild(heading);

    var content = document.createElement('p');
    content.style.color = '#eee';
    content.innerText = error || (ev && ev.message) || ev || 'Unknown Error';
    inner.appendChild(content);

    if(source){
      var sourceElement = document.createElement('p');
      sourceElement.style.color = '#777';
      sourceElement.innerText = sourceElement + ':' + lineno + ':' + clono;
      inner.appendChild(sourceElement);
    }

    document.documentElement.appendChild(element);
  }

  window.addEventListener('error', errorHandle);
  // promise没有reject处理的函数将会执行
  window.addEventListener('unhandlerejection', errorHandle);

  function makeJsTag(src, integrity){
    var script = document.createElement('script');
    script.src = src;
    script.type = 'text/javascript';
    script.charset = 'utf-8'
    // 延迟脚本的运行，当页面加载完毕后再执行。
    script.defer = true;
    if(integrity){
      script.setAttribute('integrity', integrity);
    }
    return script;
  }

  // JS文件时报错
  function loadFallbackJs(error){
    console.warn('👀 ES6脚本不支持，将加载转换后的代码');
    console.warn('👀 Error was:', error);
    // 加载转换后的代码
    // ...
  }

  function expectJsParsed(){
    if(!callbackDone){
      console.error('👀 Core未响应');
      throw new Error('Core未能在规定时间内响应');
    }
  }

  function onJsLoaded(){
    // js加载完毕时间
    console.log('👀 Core loaded at', Math.floor(performance.now()), 'ms');
    loadTimeout = setTimeout(expectJsParsed, 15000);
    window.removeEventListener('error', errorHandle);
    window.removeEventListener('unhandledrejection', errorHandle);
  }

  // 核心文件将会调用这个事件
  window.coreThreadLoadedCb = function(){
    // js响应时间
    console.log('👀 Core responsed at', Math.floor(performance.now()), 'ms');
    clearTimeout(loadTimeout);
    loadTimeout = null;
    callbackDone = true;
  }

  var scriptEs6 = makeJsTag(bundleSrc, bundleIntegrity);
  scriptEs6.addEventListener('error', loadFallbackJs);
  scriptEs6.addEventListener('load', onJsLoaded);
  document.head.appendChild(scriptEs6);
})();